/*
 * Repository.cpp
 *
 *  Created on: Sep 5, 2011
 *      Author: Patrick
 */

#include "Repository.h"

FontPathMap mcFontPathMap;
FontMap mcFontMap;
TexturePathMap mcTexturePathMap;
TextureIDMap mcTextureIDMap;

/*
 * Fonts
 */

TTF_Font* MCFontCreate(const char* name, const char* path, int size) {

	TTF_Font* font;

	if (!(font = TTF_OpenFont(path, size))) {
		printf("Error initializing font %s [%s], %s", name, path, TTF_GetError());
	}

	mcFontPathMap.insert(std::pair<const char*, const char*>(name, path));
	mcFontMap.insert(std::pair<const char*, TTF_Font*>(name, font));

	return font;

}

TTF_Font* MCFontLoad(const char* name) {

	FontMap::iterator i = mcFontMap.find(name);
	if (i != mcFontMap.end()) {
		// Return the discovered texture
		return i->second;
	}
	return NULL;

}

int MCFontDestroy() {

	FontMap::const_iterator end = mcFontMap.begin();
	for (FontMap::const_iterator i = mcFontMap.begin(); i != end; ++i) {
		TTF_CloseFont(i->second);
	}
	return 1;

}

/*
 * Textures
 */

GLuint MCTextureCreate(const char* name) {

	GLuint et = MCTextureLoad(name);
	if (et != 0) {
		// Texture already existed
		glDeleteTextures(1, &et);
	}

	// Map the path
	mcTexturePathMap.insert(std::pair<const char*, const char*>(name, NULL));

	// Map the texture
	GLuint textureID;
	glGenTextures(1, &textureID);
	mcTextureIDMap.insert(std::pair<const char*, GLuint>(name, textureID));
	return textureID;

}

GLuint MCTextureCreate(const char* name, const char* path) {

	GLuint et = MCTextureLoad(name);
	if (et != 0) {
		// Texture already existed
		glDeleteTextures(1, &et);
	}

	// Map the path
	mcTexturePathMap.insert(std::pair<const char*, const char*>(name, path));

	// Map the texture
	SDL_Surface* texture;
	GLuint textureID;
	texture = IMG_Load(path);
	if (texture != NULL) {
		SDL_LockSurface(texture);
		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pixels);
		SDL_UnlockSurface(texture);
		SDL_FreeSurface(texture);
		mcTextureIDMap.insert(std::pair<const char*, GLuint>(name, textureID));
		return textureID;
	}

	return 0;

}

GLuint MCTextureLoad(const char* name) {

	TextureIDMap::iterator i = mcTextureIDMap.find(name);
	if (i != mcTextureIDMap.end()) {
		// Return the discovered texture
		return i->second;
	}
	return 0;

}

int MCTextureDestroy() {

	int size = mcTextureIDMap.size();
	GLuint textureIDs[size];
	int index = 0;
	TextureIDMap::const_iterator end = mcTextureIDMap.begin();
	for (TextureIDMap::const_iterator i = mcTextureIDMap.begin(); i != end; ++i) {
		textureIDs[index++] = i->second;
	}
	glDeleteTextures(size, textureIDs);
	mcTexturePathMap.clear();
	mcTextureIDMap.clear();
	return 1;

}

